﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Application
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using System.Windows.Forms;

    using Mews.Application.Properties;
    using Mews.Mime;
    using Mews.Net.Nntp.Cache;

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1601:PartialElementsMustBeDocumented", Justification = "Another part already documents the class.")]
    internal partial class MainForm
    {
        /// <summary>Implements the controller for undeleted outbound articles.</summary>
        private sealed class MyArticlesController : MyArticlesControllerBase
        {
            private readonly SendStatus sendStatus;

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            /// <summary>See <see cref="IController.Document"/>.</summary>
            public sealed override object Document
            {
                get { return this.sendStatus; }
            }

            /// <summary>See <see cref="IController.UpdateCount"/>.</summary>
            public sealed override void UpdateCount()
            {
                this.MainForm.UpdateOutboundArticleCounts();
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            internal MyArticlesController(MainForm mainForm, SendStatus sendStatus) : base(mainForm, Resources.Delete)
            {
                this.sendStatus = sendStatus;
            }

            internal sealed override void OnTreeView_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    e.Handled = true;
                    this.ShowArticleForm();
                }
            }

            internal sealed override void OnTreeView_NodeTap(object sender, EventArgs e)
            {
                this.ShowArticleForm();
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            /// <summary>See <see cref="MyArticlesControllerBase.FolderName"/>.</summary>
            protected sealed override string FolderName
            {
                get { return MainForm.GetFolderName(this.sendStatus); }
            }

            /// <summary>See <see cref="MyArticlesControllerBase.GetArticles"/>.</summary>
            protected sealed override IEnumerable<IOutboundArticle> GetArticles()
            {
                return this.MainForm.cache.GetOutboundArticles(this.sendStatus);
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            private void ShowArticleForm()
            {
                var article = GetArticle(this.MainForm.treeView.SelectedNode);

                if (article == null)
                {
                    return;
                }

                var message = article.LoadMessage();
                var newsgroupsHeaderField = (UnstructuredHeaderField)message.Fields["Newsgroups"].FirstOrDefault();
                var body = (TextBody)message.Body;

                using (var form = new EditArticleForm(
                    this.MainForm,
                    article.SendStatus == SendStatus.Sent,
                    newsgroupsHeaderField.Body,
                    article.Subject,
                    this.MainForm.cache.ArticleSubjectMaxLength,
                    ((TextBody)message.Body).Content))
                {
                    var dialogResult = form.ShowDialog();

                    if (dialogResult == DialogResult.Cancel)
                    {
                        return;
                    }

                    newsgroupsHeaderField.Body = form.Groups;
                    message.Subject = form.Subject;
                    body.Content = form.Body;
                    article.SaveMessage(message);
                    var newSendStatus = dialogResult == DialogResult.Yes ? SendStatus.Outbox : SendStatus.Draft;

                    if (newSendStatus != article.SendStatus)
                    {
                        article.SendStatus = newSendStatus;
                        this.RemoveCurrentArticle();
                    }
                }
            }
        }
    }
}
